package com.gzx.plugin.tjzy.feign.file.controllrt;

import com.gzx.plugin.tjzy.feign.file.service.FileFeignService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import vip.xiaonuo.common.pojo.CommonResult;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * @author zjt
 * @description
 * @date 2024/10/18 15:26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/feign/tjzy/file")
public class FileFeignController {

    private final FileFeignService fileFeignService;



    @PostMapping("/upload")
    public CommonResult<String> upload(MultipartFile file) throws IOException {
        return CommonResult.data(fileFeignService.uploadNoTimeSuffix(file));
    }


    @GetMapping("/get")
    public byte[] getFile(@RequestParam String fileName) {
        File file = fileFeignService.getFile(fileName);
        byte[] buffer = null;
        try {
            Path path = Paths.get(file.toURI());
            buffer = Files.readAllBytes(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;
    }

}
